package com.bothin.experiment4.controller;

import com.bothin.experiment4.constant.EmployeeTypeConstant;
import com.bothin.experiment4.enums.EmployeeEnum;
import com.bothin.experiment4.model.Customer;
import com.bothin.experiment4.model.Employee;
import com.bothin.experiment4.service.ICustomerService;
import com.bothin.experiment4.utils.KeyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by bothin
 * At 2019/12/17 0:37
 * Description:
 */
@Controller
@RequestMapping("/customer")
@Secured("ROLE_USER_ADMIN")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "8") Integer size,
                             @RequestParam(value = "keyword", defaultValue = "") String keyword,
                             Map<String, Object> map){
        PageHelper.startPage(page,size);
        List<Customer> customers = null;
        if (!StringUtils.isEmpty(keyword)){
            customers = customerService.findByKeyword(keyword);
            map.put("key","&keyword="+keyword);
        }else {
            customers = customerService.findAll();
        }
        PageInfo<Customer> pageInfo = new PageInfo<>(customers);
        map.put("pageInfo", pageInfo);
        return new ModelAndView("customer/list");
    }

    @GetMapping("/to/add")
    public ModelAndView toAdd(@RequestParam(value = "customerId",required = false) String customerId,
                                      @RequestParam(value = "page",required = false) Integer page,
                                      Map<String, Object> map){
        if (!StringUtils.isEmpty(customerId)){
            Customer customer = customerService.findById(customerId);
            map.put("customer", customer);
            map.put("page", page);
        }
        return new ModelAndView("customer/add",map);
    }

    @PostMapping("/add")
    @Transactional
    public ModelAndView add(Customer customer,@RequestParam(value = "page",required = false) Integer page){
        if (StringUtils.isEmpty(customer.getCustomerId())){
            customer.setCustomerId(KeyUtil.genUniqueKey());
            customerService.insert(customer);
        }else {
            customerService.update(customer);
        }
        return new ModelAndView("redirect:/customer/list?page="+page);
    }
}
